package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderPage;
import com.itheima.pojo.OrderStatus;
import com.itheima.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberMapper memberMapper;
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Order> list = orderMapper.findPage(queryPageBean.getQueryString());
        Page<OrderPage> pageList = new Page<>();
        for (Order o : list) {
            OrderPage orderPage = new OrderPage();
            orderPage.setId(o.getId());
            orderPage.setOrderDate(o.getOrderDate());
            orderPage.setOrderStatus(o.getOrderStatus());
            orderPage.setOrderType(o.getOrderType());
            Member member = memberMapper.getById(o.getMemberId());
            orderPage.setName(member.getName());
            orderPage.setPhoneNumber(member.getPhoneNumber());
            orderPage.setFileNumber(member.getFileNumber());
            pageList.add(orderPage);
        }
        return new PageResult(pageList.getTotal(),pageList.getResult());
    }

    /**
     * 修改预约状态
     * @param orderStatus
     */
    @Override
    public void updateStatus(OrderStatus orderStatus) {
        orderMapper.updateStatus(orderStatus.getId(), orderStatus.getOrderStatus());
    }
}
